clear
set more off

global lecind    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\INDIVIDUS"
global lecindsas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\INDIVIDUS"
global ecrind    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\INDIVIDUS"

/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/*     fichier STATA 1ère livraison 2004-2008 */

foreach an of numlist 4   {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_0`an' /*pb040*/ ///
		py010n py050n py100n py090n py110n py130n py120n py140n
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 5/7   {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_0`an' pb040 ///
		py010n py050n py100n py090n py110n py130n py120n py140n
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 8  {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_0`an' pb040 ///
		py010n py050n py100n py090n py110n py130n py120n py140n
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'_bis.dta, replace
display `an'
count 
}


/* 2 - CHANGEMENT DE NOM DES VARIABLES */
foreach an of numlist 4 / 7 {
cd "$ecrind"
use indiv_`an'.dta, clear
rename py100n PY100N
rename py110n PY110N
rename py010n PY010N
rename py050n PY050N
rename py090n PY090N
rename py120n PY120N
rename py130n PY130N
rename py140n PY140N
rename idmenc_* idmenc
rename idindc_* idindc
rename pond_* pond_ind
save indiv_`an'.dta, replace
}

foreach an of numlist 8 {
cd "$ecrind"
use indiv_`an'_bis.dta, clear
rename py100n PY100N
rename py110n PY110N
rename py010n PY010N
rename py050n PY050N
rename py090n PY090N
rename py120n PY120N
rename py130n PY130N
rename py140n PY140N
rename idmenc_* idmenc
rename idindc_* idindc
rename pond_* pond_ind
save indiv_`an'_bis.dta, replace
}


/* 3 - EMPILEMENT DES TABLES */
clear 
use "$ecrind\indiv_8_bis.dta", clear
append using 	"$ecrind\indiv_4.dta" "$ecrind\indiv_5.dta" "$ecrind\indiv_6.dta" "$ecrind\indiv_7.dta"
count /* 122 742 + 17 var. */
order idmenc annee_SRCV ind men
sort idmenc annee_SRCV ind men				
save "$ecrind\individus_2004_2008.dta", replace
des
tab annee_SRCV 


/* 4 - LABELS */
clear 
use "$ecrind\individus_2004_2008.dta", clear
label variable ind				"Identifiant longitudinal de l individu"
label variable men 				"Identifiant longitudinal du menage" 
label variable annee_SRCV		"annee de l'enquete/du fichier"
label variable age				"Age de l individu a la date de l enquete"
label variable adultvrairep		"L individu a rempli un questionnaire individuel"
label variable idmenc			"Identifiant transversal du menage"
label variable idindc			"Identifiant transversal de l individu"
label variable pond_ind			"Ponderation transversale individus"
label variable pb040			"Ponderation personnelle transversale européenne"
label variable PY010N			"Salaire net percu"
label variable PY050N			"Benef/pertes nets especes activ indep"
label variable PY100N			"Montant allocations vieillesse nettes"
label variable PY090N			"Montant net allocations chomage"
label variable PY110N			"Allocations de reversion nettes"
label variable PY130N			"Pensions d'invalidite nettes"
label variable PY120N			"Indemnites de maladie nettes"
label variable PY140N			"Bourses d etudes nettes"
sort ind men annee_SRCV idmenc
order ind men annee_SRCV idmenc 
save "$ecrind\individus_2004_2008.dta", replace
tab age, missing
clear 
use "$ecrind\individus_2004_2008.dta", clear
